package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {

    private final BrandService brandService;

    public BrandController(BrandService brandService){
        this.brandService=brandService;
    }

    /**
     * 根据品牌id查询品牌
     * @param id 品牌的id
     * @return  品牌的对象
     */
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO>findBrandById(@PathVariable("id") Long id){
        Brand brandId  = brandService.getById(id);
        //转换dto
        BrandDTO brandDTO = new BrandDTO(brandId);
        return ResponseEntity.ok(brandDTO);

    }

    /**
     * 根据品牌id集合查询品牌集合
     * @param ids 品牌id集合
     * @return 品牌集合对象
     */
    @GetMapping("list")
    public ResponseEntity<List<BrandDTO>>findBrandByIds(@RequestParam("ids") List<Long>ids){
        List<Brand> brandList = brandService.listByIds(ids);
        //转换成dto
        List<BrandDTO> brandDTOS = BrandDTO.convertEntityList(brandList);
        return ResponseEntity.ok(brandDTOS);
    }

    /**
     * 分页查询品牌
     * @param key  搜搜关键字
     * @param page  当前页码
     * @param rows 每页大小
     * @return  分页结果
     */
    @GetMapping("page")
    public ResponseEntity<PageDTO<BrandDTO>>findBrandByPage(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "page" ,defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows){
        return ResponseEntity.ok(brandService.queryBrandByPage(key,page,rows));
    }

    /**
     * 根据分类id查询品牌
     * @param categoryId  分类id
     * @return  品牌的集合
     */
   @GetMapping("of/category")
    public ResponseEntity<List<BrandDTO>>queryBrandByCategoryId(@RequestParam("id") Long categoryId){
        List<BrandDTO> list = brandService.queryBrandByCategoryId(categoryId);
        return ResponseEntity.ok(list);

   }

    /**
     * 新增品牌
     * @param brandDTO 品牌数据
     * @return
     */
   @PostMapping
    public ResponseEntity<Void>saveBrand(BrandDTO brandDTO){
        brandService.saveBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
   }

    /**
     * 更新品牌
     * @param brandDTO  品牌及分类信息
     * @return
     */
   @PutMapping
    public ResponseEntity<Void>updateBrand(BrandDTO brandDTO){
       brandService.updateBrand(brandDTO);
       return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
   }


   @DeleteMapping("{id}")
    public ResponseEntity<Void>deleteBrandById(@PathVariable("id") Long id){
       brandService.deleteBrandById(id);
       return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
   }

}
